{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-05T15:20:16.605891Z",
     "start_time": "2025-05-05T15:20:12.520580Z"
    }
   },
   "source": [
    "import torch\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### Tensor 的连接操作\n",
    "在项⽬开发中，深度学习某⼀层神经元的数据可能有多个不同的来源，那么就需要将数据进⾏组合，这个组合的操作，我们称之为***连接***。\n"
   ],
   "id": "52e0b9c4322b0656"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:21:09.452368Z",
     "start_time": "2025-05-05T15:21:09.414518Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A=torch.ones(3,3)\n",
    "B=2*torch.ones(3,3)\n",
    "A"
   ],
   "id": "8b6ea43c4c46922a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:21:16.610449Z",
     "start_time": "2025-05-05T15:21:16.585933Z"
    }
   },
   "cell_type": "code",
   "source": "B",
   "id": "eb9f7077f4a86d4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2., 2., 2.],\n",
       "        [2., 2., 2.],\n",
       "        [2., 2., 2.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:23:44.637003Z",
     "start_time": "2025-05-05T15:23:44.613032Z"
    }
   },
   "cell_type": "code",
   "source": [
    "C=torch.cat((A,B),1)\n",
    "C"
   ],
   "id": "7a170454599ecb04",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 2., 2., 2.],\n",
       "        [1., 1., 1., 2., 2., 2.],\n",
       "        [1., 1., 1., 2., 2., 2.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:25:14.626002Z",
     "start_time": "2025-05-05T15:25:14.608003Z"
    }
   },
   "cell_type": "code",
   "source": [
    "D=torch.stack((A,B), 0)\n",
    "D"
   ],
   "id": "fd7766910813391d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.]],\n",
       "\n",
       "        [[2., 2., 2.],\n",
       "         [2., 2., 2.],\n",
       "         [2., 2., 2.]]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T15:27:40.586062Z",
     "start_time": "2025-05-05T15:27:40.557063Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A=torch.tensor([1,2,3,4,5,6,7,8,9,10])\n",
    "B = torch.chunk(A, 3, 0)\n",
    "B"
   ],
   "id": "681bba734169df01",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([1, 2, 3, 4]), tensor([5, 6, 7, 8]), tensor([ 9, 10]))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "<table border=\"1\" cellpadding=\"6\" cellspacing=\"0\">\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th>函数名</th>\n",
    "      <th>功能</th>\n",
    "      <th>使用要点/适用的情况</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td>torch.tensor</td>\n",
    "      <td>直接创建Tensor</td>\n",
    "      <td>一般用于简单Tensor的创建</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.from_numpy</td>\n",
    "      <td>从Numpy中创建</td>\n",
    "      <td>基于已有的Numpy数据进行Tensor创建</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.zeros</td>\n",
    "      <td>创建零矩阵Tensor</td>\n",
    "      <td rowspan=\"6\">特殊形状或者数值分布的Tensor的创建</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.ones</td>\n",
    "      <td>创建全一矩阵Tensor</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.rand</td>\n",
    "      <td rowspan=\"4\">创建随机分布Tensor</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.randn</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.normal</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.randint</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.shape</td>\n",
    "      <td rowspan=\"2\">获取Tensor形状</td>\n",
    "      <td rowspan=\"2\">注意区别函数和属性的区别</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.size</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.permute</td>\n",
    "      <td>Tensor转秩</td>\n",
    "      <td>transpose每次只能转换两个维度</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.transpose</td>\n",
    "      <td>Tensor维度交换</td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.view</td>\n",
    "      <td rowspan=\"2\">Tensor变形</td>\n",
    "      <td>view不能处理内存不连续Tensor的结构</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.reshape</td>\n",
    "      <td>reshape可以处理内存不连续Tensor的结构</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.squeeze</td>\n",
    "      <td>Tensor删除维度</td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>tensor.unsqueeze</td>\n",
    "      <td>Tensor增加维度</td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.cat</td>\n",
    "      <td>Tensor拼接</td>\n",
    "      <td>不增加维度</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.stack</td>\n",
    "      <td>Tensor堆叠</td>\n",
    "      <td>增加维度</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.chunk</td>\n",
    "      <td>Tensor尽可能平均切分</td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.split</td>\n",
    "      <td>Tensor按固定大小切分</td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.unbind</td>\n",
    "      <td>Tensor按维度切分</td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.index_select</td>\n",
    "      <td rowspan=\"2\">Tensor选择</td>\n",
    "      <td>基于给定的索引进行数据提取</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>torch.masked_select</td>\n",
    "      <td>通过一些判断条件来进行选择</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ],
   "id": "7eb5a89db0066ad9"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
